// @(#)testesms.cc
// Time-stamp: <2008-12-09 14:12:58 root>
// Copyright 2008 Julian Qian
// Author: root@localhost.localdomain
// Version: $Id: testesms.cc,v 0.0 2008/11/11 03:51:16 root Exp $
// Keywords: 

#include "testesms.h"
#include <iostream>
#include "../excep.hh"


using namespace std;

void usage(){
    cout << "Usage:" << endl;
    cout << "testesms: fetch message" << endl;
    cout << "testesms <phone-number> <message-content>: send message" << endl;
}

int main(int argc, char *argv[])
{
    if(argc < 2){
        // fetch message
        cout << "fetch message ..." << endl;
        try{
            Wrapesms *we = Wrapesms::theWrapesms();
            PHONE_PAIR *ppp;

            try{
                ppp = we->getMsg();
                PHONE_PAIR::iterator itr;
                cout<< "pp size: "<<ppp->size()<<endl;
                for(itr = ppp->begin(); itr != ppp->end(); itr++){
                    cout<<"phone: "<<itr->first<<" alarm:" << itr->second<<endl;
                }
            }
            catch(GetMsgFailed& ex){
                cout << ex.what() << endl;
            }
        }
        catch(LoginFailed& ex){
            cout << ex.what() << endl;
        }
        catch(ConfigError& ex){
            cout << ex.what() << endl;
        }

    }else if(argc == 3){
        cout << "send message..." ;
        try{
            Wrapesms *we = Wrapesms::theWrapesms();
            PHONE_PAIR ppp;

            string phone(argv[1]);
            string message(argv[2]);

            cout << phone << ": " << message << endl;

            ppp.insert(PHONE_PAIR::value_type(phone, message));            

            try{
                we->putMsg(ppp);
            }
            catch(PutMsgFailed& ex){
                cout << ex.what() << endl;
            }
        }
        catch(LoginFailed& ex){
            cout << ex.what() << endl;
        }
        catch(ConfigError& ex){
            cout << ex.what() << endl;
        }

    }else{
        usage();
    }
    
    return 0;
}


